﻿using LeanApp.Bll;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// ProbabilityCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ProbabilityCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ProbabilityCtrl(string title, SpcBll model)
        {
            InitializeComponent();
            _title = title;
            _model = model;
            InitialChart();
        }

        /// <summary>
        /// 初始化图表
        /// </summary>
        private void InitialChart()
        {
            title.Text = _title;
            if (_model != null && _model.GroupDatas != null && _model.GroupDatas.Count() > 0)
            {
                List<double> lineData = _model.GroupDatas.Select(a => a.GroupRatio).ToList();
                List<int> columnData = _model.GroupDatas.Select(a => a.ItemQty).ToList();
                List<ISeries> series = new List<ISeries>();
                ColumnSeries<int> colSeries = new ColumnSeries<int>()
                {
                    Values = columnData.ToArray(),
                    ScalesYAt = 0
                };
                LineSeries<double> lineSeries = new LineSeries<double>()
                {
                    Values = lineData.ToArray(),
                    ScalesYAt = 1
                };
                series.Add(colSeries);
                series.Add(lineSeries);
                List<Axis> xAxises = new List<Axis>();
                Axis xAxis = new Axis()
                {
                    Labels = _model.GroupDatas.Select(a => a.Index.ToString()).ToArray()
                };
                xAxises.Add(xAxis);
                List<Axis> yAxises = new List<Axis>();
                Axis yAxis0 = new Axis()
                {
                    MinLimit = 0,
                    MaxLimit = columnData.Max() + 1,
                };
                Axis yAxis1 = new Axis()
                {
                    MinLimit = lineData.Min() - 0.05 * (lineData.Max() - lineData.Min()),
                    MaxLimit = lineData.Max() + 0.05 * (lineData.Max() - lineData.Min()),
                    ShowSeparatorLines = false,
                    Position = LiveChartsCore.Measure.AxisPosition.End
                };
                yAxises.Add(yAxis0);
                yAxises.Add(yAxis1);
                chartProbabilityCtrl.Series = series;
                chartProbabilityCtrl.XAxes = xAxises;
                chartProbabilityCtrl.YAxes = yAxises;
            }
        }

        /// <summary>
        /// 数据实体
        /// </summary>
        private SpcBll _model = new SpcBll();
        /// <summary>
        /// 标题
        /// </summary>
        private string _title;
    }
}
